package com.yun.zf.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.zf.admin.mapper.ZfPactRentInfoMapper;
import com.yun.zf.admin.service.ZfPactRentInfoService;
import com.yun.zf.common.admin.domain.ZfPactRentInfo;
import com.yun.zf.common.admin.model.pact.PactRentInfoMo;
import com.yun.zf.common.mapstruct.admin.PactMapper;
import com.yun.zf.common.utils.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 合同房源信息 服务实现类
 * </p>
 *
 * @author cj
 * @since 2021-07-24
 */
@Service
public class ZfPactRentInfoServiceImpl extends ServiceImpl<ZfPactRentInfoMapper, ZfPactRentInfo> implements ZfPactRentInfoService {

    @Override
    public PactRentInfoMo pactRentInfo(Long provCode, Long cityCode, Long pactId) {
        return baseMapper.getRent(provCode, cityCode, pactId);
    }

    @Override
    public int editRentInfo(Long provCode, Long cityCode,  PactRentInfoMo rentInfoMo) {
        QueryWrapper<ZfPactRentInfo> wrapper= new QueryWrapper<>();
        wrapper.lambda().eq(ZfPactRentInfo::getPactId, Long.parseLong(rentInfoMo.getPactId())).eq(ZfPactRentInfo::getCityCode,cityCode).eq(ZfPactRentInfo::getProvinceCode,provCode);
      ZfPactRentInfo rentInfo = baseMapper.selectOne(wrapper);
      if (rentInfo==null){
           rentInfo = PactMapper.INSTANCE.moToRentInfo(rentInfoMo);
          rentInfo.setCityCode(cityCode);
          rentInfo.setProvinceCode(provCode);
          if (StringUtils.isBlank(rentInfoMo.getImplement())){
              rentInfo.setImplement(null);
          }
       return    baseMapper.insert(rentInfo);
      }
        BeanUtil.copyProperties(rentInfoMo, rentInfo, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        return baseMapper.update(rentInfo,wrapper);
    }
}
